//
//  LGFullScheduleController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/24.
//  全部日程

#import "LGFullScheduleController.h"
//病情概览
#import "LGNormalClinicController.h"
//电话咨询
#import "LGMedicineBallPhoneController.h"
//患者详情 - 圆荷健康
#import "LGPatientDetailController_YHJK.h"
#import "LGPatientDetailController2.h"

//运动方案
#import "LGPatientSportsController.h"
//聊天页面
#import "TUIC2CChatViewController.h"
#import "TUIChatConversationModel.h"

#import "LGFullScheduleHeader.h"
#import "LGFullScheduleCell.h"
@interface LGFullScheduleController ()<LGFullScheduleHeaderDelegate,LGFullScheduleDelegate,UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGFullScheduleHeader *headView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *currentDate;
/**   */
@property (nonatomic , copy)NSString *currentMonth;
/**   */
@property (nonatomic , strong)NSDateFormatter *dateFormatter;
/**   */
@property (nonatomic , strong)NSDateFormatter *monthFormatter;

@end

@implementation LGFullScheduleController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentDate = [self.dateFormatter stringFromDate:[NSDate date]];
    self.currentMonth = [self.monthFormatter stringFromDate:[NSDate date]];
    self.navigationView.title = @"全部日程";
    [self.view addSubview:self.tableView];
    self.gradientLayer.hidden = YES;
    self.view.backgroundColor = kNavBgColor;
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, viewPix(338), Screen_W, Screen_H-topBarHeight-viewPix(338))];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestMonthData];
    [self requestData];
}

-(void)requestMonthData{
    [RequestUtil withPOST:@"/doctor/workbench/getScheduleMonth" parameters:@{@"month":self.currentMonth} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.headView.pointArray = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/workbench/index" parameters:@{@"today":self.currentDate} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(code == 200){
            self.dataArray = returnData[@"list"];
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无数据" offset:viewPix(50)];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName2 content:@"暂无数据" offset:viewPix(50)];
        }
    }];
}

//切换日期
-(void)monthRecordWithYear:(NSInteger)year  month:(NSInteger)month{
    self.currentMonth= [NSString stringWithFormat:@"%ld-%.2ld",year,month];
    [self requestMonthData];
}

-(void)selectDate:(NSString *)date{
    self.currentDate = date;
    [self requestData];
}

//弹框提示
-(void)alertTipAction:(NSDictionary *)alertDic{
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"提示" content:LGNSString(alertDic[@"confirm"]) sureAction:^{
        [RequestUtil withPOST:LGNSString(alertDic[@"apiUrl"]) parameters:@{@"updateparams":alertDic[@"params"]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"保存失败，请重试" offset:0];
        }];
    }];
}

//直接请求
-(void)touchSwipeAction:(NSDictionary *)dataDic{
    [RequestUtil withPOST:LGNSString(dataDic[@"apiUrl"]) parameters:@{@"updateparams":dataDic[@"params"]} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [self requestData];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGFullScheduleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(self.dataArray.count == 1){
        cell.type = @"single";
    }else if (indexPath.row == 0){
        cell.type = @"top";
    }else if (indexPath.row == self.dataArray.count-1){
        cell.type = @"bottom";
    }else{
        cell.type = @"middle";
    }
    cell.dataDic = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    NSString *type = LGNSString(tempDic[@"type"]);
    //diseaseCode：1-房颤 、 2-肺癌 、 3-肺结节 、 4-食管癌 、5-房颤 、 7-心康管家
    NSString *diseaseCode = LGNSString(tempDic[@"disease_code"]);
    if([type isEqualToString:@"cept"]){
        //心肺运动
        LGPatientSportsController *controller = [[LGPatientSportsController alloc]init];
        controller.patientid = LGNSString(tempDic[@"patient_id"]);
        controller.diseaseCode = LGNSString(tempDic[@"disease_code"]);
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"video"] ){
        //视频咨询
        TUIChatConversationModel *model = [[TUIChatConversationModel alloc]init];
        model.title = LGNSString(tempDic[@"realname"]);
        model.userID = LGNSString(tempDic[@"member_im_id"]);
        TUIC2CChatViewController *controller = [[TUIC2CChatViewController alloc]init];
        controller.appointment_id = LGNSString(tempDic[@"appointment_id"]);
        controller.doctorid = LGNSString(tempDic[@"system_user_id"]);
        controller.roomKey = [NSString stringWithFormat:@"%@|||%@",XGUserId,model.userID];
        controller.needCustomeData = YES;
        [controller setConversationData:model];
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (![[NSObject appType:diseaseCode] isEqualToString:@"YHJK"] && ([type isEqualToString:@"visit"]||[type isEqualToString:@"followup"])){
        //心律助手、心康管家 -- 线下复查、常规门诊
        LGNormalClinicController *controller = [[LGNormalClinicController alloc]init];
        controller.patientid = LGNSString(tempDic[@"patient_id"]);
        controller.diseaseCode = diseaseCode;
        controller.levelid = LGNSString(tempDic[@"level_id"]);
        controller.visit_id = LGNSString(tempDic[@"followup_key"]);
        controller.visit_date = LGNSString(tempDic[@"today"]);
        controller.title = LGNSString(tempDic[@"title"]);
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"phone"]){
        //电话咨询
        LGMedicineBallPhoneController *controller = [[LGMedicineBallPhoneController alloc]init];
        controller.patientid = LGNSString(tempDic[@"patient_id"]);
        controller.planid = LGNSString(tempDic[@"appointment_id"]);
        controller.type = @"appoint";
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(void)itemButtonTouched:(NSDictionary *)tempDic patientid:(NSString *)patientid diseaseCode:(nonnull NSString *)diseaseCode levelid:(nonnull NSString *)levelid{
    NSString *type = LGNSString(tempDic[@"type"]);
    if([type isEqualToString:@"pinfo"]){
        //患者详情 - 圆荷健康
        if ([diseaseCode isEqualToString:@"24"] || [diseaseCode isEqualToString:@"25"]) {
            LGPatientDetailController2 *controller = [[LGPatientDetailController2 alloc]init];
            controller.diseaseCode = diseaseCode;
            controller.patientid = patientid;
            controller.levelid = levelid;
            [self.navigationController pushViewController:controller animated:YES];
        }else{
            LGPatientDetailController_YHJK *controller = [[LGPatientDetailController_YHJK alloc]init];
            controller.diseaseCode = diseaseCode;
            controller.patientid = patientid;
            controller.levelid = levelid;
            [self.navigationController pushViewController:controller animated:YES];
        }
        
    }else{
        //操作
        if(LGNSString(tempDic[@"confirm"]).length>0){
            //有弹框提示
            [self alertTipAction:tempDic];
        }else{
            //无弹框提示
            [self touchSwipeAction:tempDic];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(5);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
    headView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
    return footView;
}

-(LGFullScheduleHeader *)headView{
    if(!_headView){
        _headView = [[LGFullScheduleHeader alloc] initWithFrame:CGRectMake(-viewPix(16), 0, Screen_W, viewPix(338))];//viewPix(288)
        _headView.delegate = self;
    }
    return _headView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGFullScheduleCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor colorWithString:@"#F2F3F5"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    }
    return _tableView;
}

-(NSDateFormatter *)dateFormatter{
    if(!_dateFormatter){
        _dateFormatter = [[NSDateFormatter alloc]init];
        _dateFormatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dateFormatter;
}

-(NSDateFormatter *)monthFormatter{
    if(!_monthFormatter){
        _monthFormatter = [[NSDateFormatter alloc]init];
        _monthFormatter.dateFormat = @"yyyy-MM";
    }
    return _monthFormatter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
